package api

import (
	"fmt"
	"gitee.com/golang-lib/go-framework/core/ginx"
	"github.com/gin-gonic/gin"
	"net/http"
	"sys-admin/app/dao"
	"sys-admin/common/util"
	"time"
)

var LogCtl = new(logCtl)

type logCtl struct{}

func (s *logCtl) List(c *gin.Context) {
	var logs []dao.Log
	//db := dao.DB.Debug().Model(&dao.Log{})
	//err := db.Limit(10).Offset(0).Find(&logs).Error
	err := dao.DB.Debug().Scopes(util.Paginate(c)).Find(&logs).Error
	if err != nil {
		c.JSON(http.StatusOK, err.Error())
	} else {
		c.JSON(http.StatusOK, logs)
	}
}

func (s *logCtl) Add(c *gin.Context) {
	var log dao.Log
	ginx.BindJsonAndValid(c, &log)

	now := time.Now()
	log.CreateTime = now
	log.UpdateTime = now

	//service.LogService.Add(&log)
	result := dao.DB.Create(&log)
	fmt.Printf("影响的行数：%d", result.RowsAffected)

	c.JSON(http.StatusOK, log)
	//err := dao.DB.Debug().Create(&log).Error
}

func (s *logCtl) Update(c *gin.Context) {

}

func (s *logCtl) Delete(c *gin.Context) {

}
